﻿// ConnetAdslDlg.cpp : 茄君匡숭
//

#include "stdafx.h"
#include "ConnetAdsl.h"
#include "ConnetAdslDlg.h"
#include "ADSL.h"
#include "AdslMgr.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CADSL adsl;
CAdslMgr adsl1;
UINT AdslConnectThread(LPVOID lParam);
UINT AdslCloseThread(LPVOID lParam);

CConnetAdslDlg::CConnetAdslDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConnetAdslDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConnetAdslDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST2, m_list);
}

BEGIN_MESSAGE_MAP(CConnetAdslDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_CONNECT, &CConnetAdslDlg::OnBnClickedConnect)
	ON_BN_CLICKED(IDC_BREAK, &CConnetAdslDlg::OnBnClickedBreak)
	ON_BN_CLICKED(IDC_BUTTON1, &CConnetAdslDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CConnetAdslDlg 句口뇹잿넋埼

BOOL CConnetAdslDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 零늪뚤뺐움돨暠깃。뎠壇痰넋埼寮눗왯꼇角뚤뺐움珂，움솥쉥菱땡
	//  獵契늪꾸鱗
	SetIcon(m_hIcon, TRUE);			// 零댕暠깃
	SetIcon(m_hIcon, FALSE);		// 零鬼暠깃

	// TODO: 瞳늪警속띨棍돨놓迦뺏덜쯤
    DlgInit();
	return TRUE;  // 뇜렷쉥슝듐零돕왠숭，뤠橙럿쀼 TRUE
}

// 흔벎蕨뚤뺐움警속離鬼뺏객큐，橙矜狼苟충돨덜쯤
//  윱삥齡맡暠깃。뚤黨賈痰匡도/柬暠친謹돨 MFC 壇痰넋埼，
//  侶쉥譚움솥菱땡供냥。

void CConnetAdslDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 痰黨삥齡돨구苟匡

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 賈暠깃瞳묏鱗앤近櫓앙櫓
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 삥齡暠깃
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//뎠痰빵賈땡離鬼뺏눗왯珂溝固딧痰늪변鑒혤돤밟깃鞫刻。
//
HCURSOR CConnetAdslDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CConnetAdslDlg::OnBnClickedConnect()
{
	AfxBeginThread(AdslConnectThread,this);
}

void CConnetAdslDlg::OnBnClickedBreak()
{
	AfxBeginThread(AdslCloseThread,this);
}

void CConnetAdslDlg::DlgInit()
{
	if(adsl.GetConnectState())
	{
		GetDlgItem(IDC_CONNECT)->EnableWindow(false);
		adsl.GetAdslStyle();
	}
	else
	{
		GetDlgItem(IDC_CONNECT)->EnableWindow(true);
	}
    SetDlgItemText(IDC_ADSLIP,adsl.GetAdslIp());
	adsl.OnEnum();
	SetDlgItemText(IDC_EDIT4,adsl.GetAdslName());
}

UINT AdslConnectThread(LPVOID lParam)
{
	CConnetAdslDlg *pDlg=(CConnetAdslDlg*)lParam;
	CString strEntryName=_T("");
	pDlg->GetDlgItemText(IDC_EDIT4,strEntryName);
	TCHAR szUserName[MAX_PATH], szPassword[MAX_PATH];
	pDlg->GetDlgItemText(IDC_USER,szUserName,MAX_PATH);
	pDlg->GetDlgItemText(IDC_PASS,szPassword,MAX_PATH);

	//adsl1.RAS_Connect(szUserName, szPassword, strEntryName.GetBuffer()))
	if(adsl.RAS_Connect(szUserName,szPassword,strEntryName.GetBuffer()))
	{
		//pDlg->GetDlgItem(IDC_CONNECT)->EnableWindow(false);
		//pDlg->GetDlgItem(IDC_BREAK)->EnableWindow(true);
	}
	pDlg->SetDlgItemText(IDC_ADSLIP,adsl.GetAdslIp());
	pDlg->SetDlgItemText(IDC_STATIC_NETTYPE,adsl.GetRetInfo());
	strEntryName.ReleaseBuffer();
	return 0;
}
UINT AdslCloseThread(LPVOID lParam)
{
	CConnetAdslDlg *pDlg=(CConnetAdslDlg*)lParam;

	adsl.RAS_Close();

	//pDlg->GetDlgItem(IDC_CONNECT)->EnableWindow(true);
	//pDlg->GetDlgItem(IDC_BREAK)->EnableWindow(false);
	pDlg->SetDlgItemText(IDC_STATIC_NETTYPE,adsl.GetRetInfo());
	return 0;
}

void CConnetAdslDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strEntryName = _T("");
	CString szUserName, szPassword;
	GetDlgItemText(IDC_EDIT4, strEntryName);
	GetDlgItemText(IDC_USER, szUserName);
	GetDlgItemText(IDC_PASS, szPassword);
	adsl1.CreatePPPoE(strEntryName, szUserName, szPassword);
}
